Skip to content

Method: writeGenClass(GenClass, File)

1: package de.fhdw.wtf.generator.writer.writer;
2:
3: import java.io.File;
4: import java.io.IOException;
5: import java.io.StringWriter;
6:
7: import org.apache.velocity.Template;
8: import org.apache.velocity.VelocityContext;
9: import org.apache.velocity.app.VelocityEngine;
10: import org.apache.velocity.context.Context;
11: import org.apache.velocity.runtime.RuntimeConstants;
12: import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
13:
14: import de.fhdw.wtf.file.FileUtils;
15: import de.fhdw.wtf.generator.java.generatorModel.GenClass;
16:
17: public class FileHeaderWriter {
18:         
19:         private final ClassFileWriter classFileWriter;
20:         
21:         private static final String PACKAGE_KEY = "package";
22:         private static final String IMPORTS_KEY = "imports";
23:         private static final String GEN_CLASS_KEY = "genclass";
24:         private static final char JAVA_LINE_END = ';';
25:         private static final char PACKAGE_PATH_SEP = '.';
26:         private static final char FILE_PATH_SEP = '/';
27:         
28:         private static final String TEMPLATE_FILE_NAME = "de/fhdw/wtf/generator/templates/fileheader.vm";
29:         
30:         private final Context context = new VelocityContext();
31:         private final Template template;
32:         private final VelocityEngine engine;
33:         
34:         public FileHeaderWriter(final ClassFileWriter classFileWriter) {
35:                 this.classFileWriter = classFileWriter;
36:                 this.engine = new VelocityEngine();
37:                 this.engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
38:                 this.engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
39:                 this.engine.init();
40:                 this.template = this.engine.getTemplate(TEMPLATE_FILE_NAME);
41:         }
42:         
43:         public void writeGenClass(final GenClass genclass, final File rootDir) {
44:                 this.setUpContext(genclass);
45:                 this.writeToFile(genclass, rootDir);
46:         }
47:         
48:         private void setUpContext(final GenClass c) {
49:                 this.setPackage(c, this.context);
50:                 this.setImports(c, this.context);
51:                 this.setGenClass(c, this.context);
52:         }
53:         
54:         /**
55:          * Writes the file for the given {@link GenClass} <code>c</code>.
56:          *
57:          * @param rootDir
58:          */
59:         private void writeToFile(final GenClass c, final File rootDir) {
60:                 final String result = this.generateFileContent(this.context);
61:                 final File path = this.createPath(c, rootDir);
62:                 final File file = this.createFile(path, c);
63:                 this.writeFile(result, path, file);
64:         }
65:         
66:         /**
67:          * Merges the context into the classtemplate.
68:          *
69:          * @param context
70:          * @return
71:          */
72:         private String generateFileContent(final Context context) {
73:                 final StringWriter writer = new StringWriter();
74:                 this.template.merge(context, writer);
75:                 return writer.toString();
76:         }
77:         
78:         /**
79:          * Creates the Path {@link File} for c.
80:          *
81:          * @param c
82:          */
83:         private File createPath(final GenClass c, final File rootDir) {
84:                 String path = c.getPackag().toString();
85:                 path = path.replace(PACKAGE_PATH_SEP, FILE_PATH_SEP);
86:                 path = path.replace(JAVA_LINE_END, FILE_PATH_SEP);
87:                 return new File(rootDir.getAbsolutePath() + FILE_PATH_SEP + path);
88:         }
89:         
90:         /**
91:          * Creates the {@link File} for class c.
92:          *
93:          * @param path
94:          * @param c
95:          */
96:         private File createFile(final File path, final GenClass c) {
97:                 final String file = path.getAbsolutePath() + FILE_PATH_SEP + c.getName() + c.getFileEnding();
98:                 return new File(file);
99:         }
100:         
101:         /**
102:          * Writes the given result into the file.
103:          *
104:          * @param result
105:          * @param path
106:          * @param file
107:          */
108:         private void writeFile(final String result, final File path, final File file) {
109:                 try {
110:                         System.out.println(" --> " + file.getAbsolutePath());
111:                         FileUtils.overrideToFile(result, path, file);
112:                 } catch (final IOException e) {
113:                         e.printStackTrace();
114:                 }
115:         }
116:         
117:         /**
118:          * Sets the imports block for c.
119:          *
120:          * @param c
121:          * @param context
122:          */
123:         private void setImports(final GenClass c, final Context context) {
124:                 context.put(IMPORTS_KEY, c.getImports());
125:         }
126:         
127:         /**
128:          * Sets all values for the package declaration of {@link GenClass} c to the {@link Context}.
129:          *
130:          * @param c
131:          * @param context
132:          */
133:         private void setPackage(final GenClass c, final Context context) {
134:                 context.put(PACKAGE_KEY, c.getPackag().toString() + JAVA_LINE_END);
135:         }
136:         
137:         /**
138:          * Sets the content of the containing type c.
139:          *
140:          * @param c
141:          * @param context
142:          */
143:         private void setGenClass(final GenClass c, final Context context) {
144:                 context.put(GEN_CLASS_KEY, this.classFileWriter.getStringContent(c));
145:         }
146: }